//
//  DataBase.m
//  Funny
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 King. All rights reserved.
//

#import "DataBase.h"

@implementation DataBase

+ (DataBase *)shareDataBase
{
    static DataBase *dataBase = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        dataBase = [[DataBase alloc]init];
    });
    return dataBase;
}

- (BOOL)open
{
   BOOL result = [self.dataBase open];
    return result;
}

- (BOOL)close
{
    BOOL result = [self.dataBase close];
    return result;
}

- (FMDatabase *)dataBase
{
    if (!_dataBase) {
        NSString *path = [NSString stringWithFormat:@"%@/Documents/first.rdb",NSHomeDirectory()];
        self.dataBase = [[FMDatabase alloc]initWithPath:path];
        
    }
    return _dataBase;
}

- (BOOL)creatTable
{
    BOOL open = [self.dataBase executeUpdate:[NSString stringWithFormat:@"create table CityInfo (cityID text primary key, content text, pic text)"]];
    return open;
}


- (BOOL)insertDataWithModel:(MainIntroductionModel *)model
{
    BOOL insertResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"insert into CityInfo values ('%@','%@','%@')",model.cityID,model.brief,model.mainpic]];
    return insertResult;
}

- (BOOL)deleteDataWithModel:(MainIntroductionModel *)model
{
    BOOL deleteResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"delete from CityInfo where cityID = '%@'",model.cityID]];
    return deleteResult;
}

- (BOOL)upDataWithNewModel:(MainIntroductionModel *)model
{
    BOOL updataResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"update CityInfo set CityInfo = '%@' where content = '%@' and pic = '%@'",model.cityID,model.brief,model.mainpic]];
    return updataResult;
}
- (NSArray *)getAll
{
    FMResultSet *set = [self.dataBase executeQuery:@"SELECT * FROM CityInfo"];
    NSMutableArray *arr = [NSMutableArray array];
    while ([set next]) {
        MainIntroductionModel *model = [[MainIntroductionModel alloc]init];
        model.cityID = [set stringForColumn:@"cityID"];
        model.brief = [set stringForColumn:@"content"];
        model.mainpic = [set stringForColumn:@"pic"];
        [arr addObject:model];
    }
    return arr;
}


- (BOOL) creatDetailTable
{
    BOOL open = [self.dataBase executeUpdate:[NSString stringWithFormat:@"create table DetailInfo (productID text primary key, content text, pic text)"]];
    return open;
}

- (BOOL)insertDetailDataWithModel:(Detail *)model
{
    BOOL insertResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"insert into DetailInfo values ('%@','%@','%@')",model.cityID,model.productName,model.imageurl]];
    return insertResult;
}

- (BOOL)deleteDetailDataWithModel:(Detail *)model
{
    BOOL deleteResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"delete from DetailInfo where productID = '%@'",model.cityID]];
    return deleteResult;
}

- (BOOL)upDetailDataWithNewModel:(Detail *)model
{
    BOOL updataResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"update DetailInfo set productID = '%@' where content = '%@' and pic = '%@'",model.cityID,model.productName,model.imageurl]];
    return updataResult;
}

- (NSArray *)getDetailAll
{
    FMResultSet *set = [self.dataBase executeQuery:@"SELECT * FROM DetailInfo"];
    NSMutableArray *arr = [NSMutableArray array];
    while ([set next]) {
        Detail *model = [[Detail alloc]init];
        NSString *str = [set stringForColumn:@"productID"];
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        model.cityID = [numberFormatter numberFromString:str];
        model.productName = [set stringForColumn:@"content"];
        NSString *str1 = [model.imageList.firstObject objectForKey:@"url"];
        str1 = [set stringForColumn:@"pic"];
        model.imageurl = [set stringForColumn:@"pic"];
        [arr addObject:model];
    }
    return arr;
}

- (BOOL) creatInlandTable
{
    BOOL open = [self.dataBase executeUpdate:[NSString stringWithFormat:@"create table InlandInfo (productID text primary key, content text, pic text)"]];
    return open;
}

- (BOOL)insertInlandDataWithModel:(FoodInfoModel *)model
{
    BOOL insertResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"insert into InlandInfo values ('%@','%@','%@')",model.cityID,model.content,model.pic]];
    return insertResult;
}

- (BOOL)deleteInlandDataWithModel:(FoodInfoModel *)model
{
    BOOL deleteResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"delete from InlandInfo where productID = '%@'",model.cityID]];
    return deleteResult;
}

- (BOOL)updateInlandDataWithNewModel:(FoodInfoModel *)model
{
    BOOL updataResult = [self.dataBase executeUpdate:[NSString stringWithFormat:@"update InlandInfo set productID = '%@' where content = '%@' and pic = '%@'",model.cityID,model.content,model.pic]];
    return updataResult;
}

- (NSArray *)getInlandAll
{
    FMResultSet *set = [self.dataBase executeQuery:@"SELECT * FROM InlandInfo"];
    NSMutableArray *arr = [NSMutableArray array];
    while ([set next]) {
        FoodInfoModel *model = [[FoodInfoModel alloc]init];
        model.cityID = [set stringForColumn:@"productID"];
        model.content = [set stringForColumn:@"content"];
        model.pic = [set stringForColumn:@"pic"];
        [arr addObject:model];
    }
    return arr;
}

@end
